# Copyright 2025 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@aspect_rules_js//js:defs.bzl", "js_run_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
load("//pw_build:pw_cc_blob_library.bzl", "pw_cc_blob_info", "pw_cc_blob_library")

package(default_visibility = ["//pw_async2/codelab:__subpackages__"])

licenses(["notice"])

# Bundle the web app using Rollup
js_run_binary(
    name = "webui_js",
    srcs = [
        "rollup.config.js",
        "tsconfig.json",
        "www/main.ts",
        "//:node_modules",
    ],
    outs = ["www/main.js"],
    args = [
        "-c",
        "rollup.config.js",
    ],
    chdir = package_name(),
    target_compatible_with = incompatible_with_mcu(),
    tool = "//:rollup_bin",
    visibility = ["//visibility:private"],
)

pw_cc_blob_info(
    name = "main_js_blob",
    file_path = ":webui_js",
    symbol_name = "main_js",
    visibility = ["//visibility:private"],
)

pw_cc_blob_info(
    name = "main_css_blob",
    file_path = "www/main.css",
    symbol_name = "main_css",
    visibility = ["//visibility:private"],
)

pw_cc_blob_info(
    name = "index_html_blob",
    file_path = "www/index.html",
    symbol_name = "index_html",
    visibility = ["//visibility:private"],
)

pw_cc_blob_library(
    name = "webui_resources",
    blobs = [
        ":main_js_blob",
        ":main_css_blob",
        ":index_html_blob",
    ],
    namespace = "codelab::webui::resources",
    out_header = "webui/resources.h",
    visibility = ["//visibility:private"],
)

cc_library(
    name = "webui_server",
    srcs = ["cc/webui_server.cc"],
    hdrs = ["cc/public/webui/webui_server.h"],
    implementation_deps = [
        ":webui_resources",
        "//pw_assert:check",
        "//pw_log",
        "//pw_preprocessor",
        "//pw_result",
        "//pw_span",
        "//pw_status",
        "//pw_stream:socket_stream",
        "//pw_string:builder",
    ],
    strip_include_prefix = "cc/public",
    visibility = ["//pw_async2/codelab:__subpackages__"],
)
